package MelbourneMap;

import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 * Helicopter is an object that can move (fly)
 * between two specified points on the map.
 * A helicopter runs in its own threads and 
 * moves across the map from the specified start
 * location to the specified end location.
 * 
 * @author David Zappia, 7183216
 * @author Heong Jin Yu, 7019777
 * 
 */
public class Helicopter extends NotifyingThread
{
	private Graph graph;
	private Point2D start, target;
	private double xPos, yPos;
	private Shape shape;
	private final int diameter = 200;
	
	/**
	 * Class constructor specifying the start and target objects.
	 * 
	 * @param startLoc		the location to start at
	 * @param targetLoc		the location to be end at
	 */
	public Helicopter(Point2D startLoc, Point2D targetLoc)
	{
		start = startLoc;
		target = targetLoc;
		shape = new Ellipse2D.Double(xPos-(diameter/2), yPos-(diameter/2), diameter, diameter);
	}
	
	/**
	 * Traverses the path between the two points
	 * specified during construction
	 */
	@Override
	public void doRun() 
	{
        long startTime = System.currentTimeMillis();
		traversePath();
        long endTime = System.currentTimeMillis();
		System.out.println("Done! " + (endTime - startTime));
	}
	
	
	/**
	 * Moves the Helicopter between the two points
	 * using basic linear mathematics to increment 
	 * the x and y positions of the helicopter on 
	 * the map.
	 */
	private void traversePath()
	{
		xPos = start.getX();
		yPos = start.getY();
		double dx = target.getX() - xPos;
		double dy = target.getY() - yPos;
		double D = Math.sqrt((dx*dx) + (dy*dy));
		double xInc = (dx/D);
		double yInc = (dy/D);
		
		while (Math.round(xPos) != target.getX())
		{
			xPos += xInc;
			yPos += yInc;
			shape = new Ellipse2D.Double(xPos-(diameter/2), yPos-(diameter/2), diameter, diameter);
			
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
